package com.daisuxia.web.spider;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

//import org.apache.catalina.util.URLEncoder;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.daisuxia.constant.Constant;

public class RequestUtil {

	private static Logger loger = Logger.getLogger(RequestUtil.class);

	/**
	 * 输入参数转json
	 * 
	 * @param requestParam
	 * @return
	 */
	public static JSONObject loadRequest(String requestParam) {
		JSONObject jsonObj = stringToJson(requestParam);
		return jsonObj;
	}

	public static JSONObject stringToJson(String requestParam) {
		JSONObject jsonObj = new JSONObject();
		jsonObj = jsonObj.fromObject(requestParam);
		return jsonObj;
	}

	public static Map<String, Object> loadInfoValue(String infoValue,
			String parseType) {
		if (parseType.equals(Constant.SCHEME_STATUS_ONE)) {
			return loadInfoValueByList(infoValue);
		} else if (parseType.equals(Constant.SCHEME_STATUS_TWO)) {
			return loadInfoValueByItem(infoValue);
		}
		return null;
	}

	/**
	 * 获取列表输入参数
	 * 
	 * @param infoValue
	 * @return
	 */
	public static Map<String, Object> loadInfoValueByList(String infoValue) {
		HashMap<String, Object> map = new HashMap<String, Object>();

		List<String> list = new ArrayList<String>();
		List<String> siteList = new ArrayList<String>();
		JSONObject jsonObject = RequestUtil.loadRequest(infoValue);

		JSONArray jsonArray = jsonObject.getJSONArray("sources");

		String page = jsonObject.getString("page") == null ? "1" : jsonObject
				.getString("page");// 默认为1
		map.put("page", page);
		String url = "";
		String keyWord = jsonObject.getString("keyWord");
		map.put(Constant.KEYWORD_MAP_KEY, keyWord);

		for (Object obj : jsonArray) {
			page = getPage(String.valueOf(obj), jsonObject.getString("page"));
			url = getUrl(String.valueOf(obj), page, keyWord);
			list.add(url);
			map.put("sources", list);
		}
		return map;
	}

	/**
	 * 获取列表参数
	 * 
	 * @param infoValue
	 * @return
	 */
	public static Map<String, Object> loadInfoValueByItem(String infoValue) {
		HashMap<String, Object> map = new HashMap<String, Object>();

		List<String> list = new ArrayList<String>();
		JSONObject jsonObject = RequestUtil.loadRequest(infoValue);
		JSONArray jsonArray = jsonObject.getJSONArray("keys");
		for (Object obj : jsonArray) {
			list.add(String.valueOf(obj));
		}
		map.put("list", list);
		String url = jsonObject.getString("url");
		map.put("url", url);
		return map;
	}

	/**
	 * 获取分页数
	 * 
	 * @param param
	 * @param page
	 * @return
	 */
	public static String getPage(String param, String page) {
		if (StringUtils.isBlank(page)) {
			return "1";
		}
		if (param.equals(Constant.WDTY_LIST)) {
			return page;
		} else if (param.equals(Constant.WDZJ_LIST)) {// JINGDONG
			return page;
		}
		return page;
	}

	/**
	 * 获取url
	 * 
	 * @param param
	 * @param page
	 * @param keyWord
	 * @return
	 */
	public static String getUrl(String param, String page, String keyWord) {
		String url = "";
		if (param.equals(Constant.WDTY_LIST)) {// TIANMAO
			url = Constant.WDTY_LIST;
			loger.info("源url=" + url);
			// String newStr = new URLEncoder().encode(keyWord);
			String newStr = keyWord;
			loger.info("tianmaonewStr=" + newStr);
			url = url.replace("bwmbwmbwm", newStr);// 更换keyword
			loger.info("keywordurl=" + url);
			url = url.replace("bwmnumpage", page);// 更换页数
			loger.info("pageurl=" + url);
			return url;
		} else if (param.equals(Constant.WDZJ_LIST)) {// JINGDONG
			url = Constant.WDZJ_LIST;
			loger.info("源url=" + url);
			// String newStr = new URLEncoder().encode(keyWord);
			String newStr = keyWord;
			loger.info("jdnewStr=" + newStr);
			url = url.replace("bwmbwmbwm", newStr);// 更换keyword
			loger.info("keywordurl=" + url);
			url = url.replace("bwmnumpage", page);// 更换页数
			loger.info("pageurl=" + url);
			return url;
		}
		return url;
	}
}
